<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <!-- 搜索区域 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <div class="grid-content bg-purple">
            <el-input
              placeholder="请输入内容"
              v-model="queryInfo.query"
              clearable
              @keyup.enter.native="searchHandleBtn"
              @clear="searchHandleBtn"
            >
              <el-button slot="append" icon="el-icon-search" @click="searchHandleBtn"></el-button>
            </el-input>
          </div>
        </el-col>
      </el-row>
      <!-- 表格区域 -->
      <el-table :data="ordersList" border style="width: 100%">
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="order_number" label="订单编号"></el-table-column>
        <el-table-column prop="order_price" label="订单价格(元)" width="95"></el-table-column>
        <el-table-column prop="order_pay" label="是否付款" width="100">
          <template slot-scope="scope">
            <el-button
              v-if="scope.row.order_pay === '0'"
              type="danger"
              disabled
              size="mini"
              plain
            >未付款</el-button>
            <el-button
              v-else-if="scope.row.order_pay === '1'"
              type="success"
              disabled
              size="mini"
              plain
            >已付款</el-button>
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货" width="140"></el-table-column>
        <el-table-column label="下单时间" width="140">
          <template slot-scope="scope">{{scope.row.update_time | dateFormat}}</template>
        </el-table-column>
        <el-table-column label="操作" width="130">
          <template>
            <!-- 修改按钮 -->
            <el-button type="primary" size="mini" icon="el-icon-edit" @click="showBox"></el-button>
            <!-- 展示物流信息 -->
            <el-button
              type="success"
              size="mini"
              icon="el-icon-location-outline"
              @click="showProgressBox"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页功能 -->
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pagenum"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="queryInfo.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
      </div>
      <!-- 修改地址的弹出层 -->
      <el-dialog title="修改地址" :visible.sync="addressVisible" width="50%" @close="handleClose">
        <el-form
          :model="addressForm"
          :rules="addressFormRules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="省市区/县" prop="address1">
            <el-cascader
              v-model="addressForm.address1"
              :options="cityDate"
              :props="{ expandTrigger: 'hover' }"
            ></el-cascader>
          </el-form-item>
          <el-form-item label="详细地址" prop="address2">
            <el-input v-model="addressForm.address2"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="addressVisible = false">取 消</el-button>
          <el-button type="primary" @click="addressVisible = false">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 展示物流进度的对话框 -->
      <el-dialog
        title="物流进度"
        :visible.sync="progressVisible"
        width="50%"
        @close="progressVisibleClosed"
      >
        <el-timeline>
          <el-timeline-item
            v-for="(activity, index) in progressInfo"
            :key="index"
            :timestamp="activity.ftime"
          >{{activity.context}}</el-timeline-item>
        </el-timeline>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import city from "./citydata.js";
import { truncate } from "fs";
export default {
  data() {
    return {
      // 搜索的信息
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 10
      },
      ordersList: [],
      // 总页码
      total: 0,
      // 控制修改地址的弹出层
      addressVisible: false,
      // 控制展示物流进度的弹出层
      progressVisible: false,
      // 获取的物流进度的数据
      progressInfo: [],
      // 修改地址渲染的数据
      addressForm: {
        // 被选中的省市区
        address1: "",
        address2: ""
      },

      addressFormRules: {
        address1: [
          { required: true, message: "请选择省市区/县", trigger: "blur" }
        ],
        address2: [
          { required: true, message: "请填写详细地址", trigger: "blur" }
        ]
      },
      // 省市区的数据
      cityDate: city
    };
  },
  created() {
    this.getOrdersList();
  },
  methods: {
    // 请求订单列表的数据
    async getOrdersList() {
      const { data: res } = await this.$http.get("orders", {
        params: this.queryInfo
      });
      // console.log(res);
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      // console.log(res);
      this.ordersList = res.data.goods;
      this.total = res.data.total;
    },
    // 搜索框的事件
    searchHandleBtn() {
      this.getOrdersList();
    },
    // 分页的页码条数改变事件
    handleSizeChange(v) {
      this.queryInfo.pagesize = v;
      this.getOrdersList();
    },
    // 分页的当前页改变事件
    handleCurrentChange(v) {
      this.queryInfo.pagenum = v;
      this.getOrdersList();
    },
    // 展开修改地址的弹出框
    showBox() {
      this.addressVisible = true;
    },
    // 修改地址弹出层关闭后重置表单
    handleClose() {
      this.$refs.ruleForm.resetFields();
      this.addressVisible = false;
    },
    // 展示物流信息的点击事件
    async showProgressBox() {
      this.progressVisible = true;
      const { data: res } = await this.$http.get("/kuaidi/804909574412544580");
      console.log(res);
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.progressInfo = res.data;
      console.log(this.progressInfo);
    },
    progressVisibleClosed() {
      console.log(1);
      this.progressInfo = [];
    }
  }
};
</script>

<style lang="less" scoped>
.el-cascader {
  width: 100%;
}
</style>
